package cn.alusiaor.springbootsecurity.exception;


import cn.alusiaor.common.util.ExceptionUtil;
import cn.alusiaor.springbootsecurity.entity.ModelResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;


@ControllerAdvice
@Slf4j
public class ExceptionHandle {


    @ExceptionHandler(value = ConstraintViolationException.class)
    @ResponseBody
    public ModelResult handleConstraint(ConstraintViolationException e) {
        // 获取该次请求上下文
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //获取校验错误信息
        StringBuilder message = new StringBuilder();
        Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
        for (ConstraintViolation<?> violation : violations) {
            message.append(violation.getMessage()).append(",");
        }
        message = new StringBuilder(message.substring(0, message.length() - 1));
        String exceptionMsg = message.toString();
        //记录异常
        ExceptionUtil.printExceptionMessage("参数校验异常", exceptionMsg, e, request);
        //返回结果
        return ModelResult.failRspOnlyMsg(exceptionMsg);
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public ModelResult handleMethodArgumentNotValid(MethodArgumentNotValidException e) {
        // 获取该次请求上下文
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //获取校验错误信息
        StringBuilder message = new StringBuilder();
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        for (FieldError error : fieldErrors) {
            message.append(error.getDefaultMessage()).append(",");
        }
        message = new StringBuilder(message.substring(0, message.length() - 1));
        String exceptionMsg = message.toString();
        //记录异常
        ExceptionUtil.printExceptionMessage("参数校验异常", exceptionMsg, e, request);
        //返回结果
        return ModelResult.failRspOnlyMsg(exceptionMsg);
    }

    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public ModelResult handleBindException(BindException e) {
        // 获取该次请求上下文
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //获取校验错误信息
        StringBuilder message = new StringBuilder();
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        for (FieldError error : fieldErrors) {
            message.append(error.getDefaultMessage()).append(",");
        }
        message = new StringBuilder(message.substring(0, message.length() - 1));
        String exceptionMsg = message.toString();
        //记录异常
        ExceptionUtil.printExceptionMessage("参数校验异常", exceptionMsg, e, request);
        //返回结果
        return ModelResult.failRspOnlyMsg(exceptionMsg);
    }

    @ExceptionHandler(value = MyException.class)
    @ResponseBody
    public ModelResult handle(MyException e) {
        // 获取该次请求上下文
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //记录异常
        ExceptionUtil.printExceptionMessage("业务异常", e.getMsg(), e, request);
        //返回结果
        return ModelResult.failRspCodeAndMsg(e.getCode(), e.getMsg());
    }

   
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ModelResult handle(Exception e) {
        // 获取该次请求上下文
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //记录异常
        ExceptionUtil.printExceptionMessage("服务异常", e.getMessage(), e, request);
        //返回结果
        return ModelResult.failRspOnlyMsg("服务异常");
    }
}